﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Security;

namespace OOSE_TDD
{
    public class Email : IEmail
    {
        #region fields
        private string fromAdress = "joey-man.joden@web.de";
        private string toAdress = "joey-man.joden@web.de";
        private string subject = "Bestellung";
        private string body = "";
        private int smtpPort = 25;
        private string smtpHost = "smtp.web.de";
        private string password = "1214!K17f";
        private bool varIsSend = false;
 
        
        #endregion

        #region properties
        public string FromAdress
        {
            get { return fromAdress; }
            set { fromAdress = value; }
        }

        public string ToAdress
        {
            get { return toAdress; }
            set { toAdress = value; }
        }

        public string Subject
        {
            get { return subject; }
            set { subject = value; }
        }
     
        public string Body
        {
            get { return body; }
            set { body = value; }
        }

        public int SmtpPort
        {
            get { return smtpPort; }
            set { smtpPort = value; }
        }
        
        public string SmtpHost
        {
            get { return smtpHost; }
            set { smtpHost = value; }
        }
        
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        #endregion

        // this method send a mail via smtp-server
        public bool isSend(Customer myCustomer, Article myArticle)
        {
            // builds the string for the email body
            body =  myCustomer.FirstName + " " + myCustomer.LastName + Environment.NewLine + 
                    myCustomer.Street + Environment.NewLine + 
                    myCustomer.PostCode + " " + myCustomer.City + Environment.NewLine +
                    Environment.NewLine + Environment.NewLine +
                    "Vielen Dank für Ihre Bestellung bei Webshop!" + Environment.NewLine + 
                    Environment.NewLine +
                    "Sie haben folgendes bestellt:" + Environment.NewLine + 
                    Environment.NewLine +
                    "Artikelname: " + myArticle.ArticleName + Environment.NewLine +
                    "Bruttopreis: " + myArticle.GrossPrice + " € " + Environment.NewLine +
                    "Mehrwertsteuer: " + myArticle.TaxValue + " € " + Environment.NewLine +
                    "Nettopreis: " + myArticle.NetPrice + "€" + Environment.NewLine + 
                    Environment.NewLine +
                    Environment.NewLine +
                    "Mit freundlichen Grüßen" + Environment.NewLine +
                    Environment.NewLine +
                    "Ihr Team von Webshop";

            // creates a message
            MailMessage myMessage = new MailMessage(fromAdress, toAdress, subject, body);
            //creates the networkCredentials to pass the server
            NetworkCredential myCredential = new NetworkCredential(fromAdress, password);
            
            //creates a client which sends the message to the receiver
            SmtpClient myClient = new SmtpClient(smtpHost, smtpPort);
            // saves the credentials to the client
            myClient.Credentials = myCredential;
            //sends mail#
            try
            {
                myClient.Send(myMessage);
                varIsSend = true;

            }
            catch
            {
                Console.WriteLine("Feler: Nachricht konnte nicht versendet werden.");
                varIsSend = false;
            }

            return varIsSend;
        }


    }
}
